package net.wofly.sale.commodity.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.transaction.Transactional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import net.wofly.common.condition.Condition;
import net.wofly.common.condition.ConditionRelate;
import net.wofly.common.condition.ConditionUtil;
import net.wofly.common.condition.FilterCondition;
import net.wofly.common.condition.Operation;
import net.wofly.common.json.XiNiaoJSON;
import net.wofly.framework.dao.PageWrapper;
import net.wofly.framework.util.Identities;
import net.wofly.framework.util.PageHelper;
import net.wofly.framework.util.ReturnBean;
import net.wofly.sale.commodity.domain.Brand;
import net.wofly.sale.commodity.domain.CommodityDetailImg;
import net.wofly.sale.commodity.repository.BrandRepository;
import net.wofly.sale.commodity.service.IBrandService;
import net.wofly.sale.commodity.service.ICommodityDetailImgService;
import net.wofly.sale.order.domain.Order;

@Service("iBrandService")
@Transactional
public class BrandServiceImpl implements IBrandService {

	Logger log = LoggerFactory.getLogger(BrandServiceImpl.class);

	public static final String CACHE_NAME = "brand";

	@Autowired
	BrandRepository brandRepository;
	
	@Autowired
	ICommodityDetailImgService iCommodityDetailImgService ;

	@Override
	public ReturnBean<Brand> save(Brand brand) {
		
		brand.setBrandID(Identities.uuid2());
		brand.setStatus(1);
		brand.setModifyDate(System.currentTimeMillis());
		
		saveDetailImgs(brand) ;
		
		return new ReturnBean<>(0, "添加商品信息成功", brandRepository.save(brand));
	}
	
	@Override
	public ReturnBean<Brand> update(Brand brand) {
		Brand dbBrand = brandRepository.findOne(brand.getBrandID()) ;
		
		if (null == dbBrand) {
			return new ReturnBean<>(-1, "更新品牌信息不存在");
		}
		
		brand.setStatus(1);
		brand.setModifyDate(System.currentTimeMillis());
		
		saveDetailImgs(brand) ;
		
		return new ReturnBean<>(0, "添加商品信息成功", brandRepository.save(brand));
	}
	
	public ReturnBean<String> saveDetailImgs(Brand brand) {
		List<CommodityDetailImg> commodityDetailImgs  = brand.getCommodityDetailImgs();
		
		if(null == commodityDetailImgs || commodityDetailImgs.isEmpty()) {
			return new ReturnBean<>(ReturnBean.CODE_SUCCESS, "保存品牌详情图片成功") ;
		}
		
		for(CommodityDetailImg commodityDetailImg :commodityDetailImgs) {
			commodityDetailImg.setCommodityDetailImgID(Identities.uuid2());
			commodityDetailImg.setCommodityID(brand.getBrandID());
			commodityDetailImg.setImgType(2);
			commodityDetailImg.setModifyDate(brand.getModifyDate());
		}
		
		iCommodityDetailImgService.deleteByCommodityID(brand.getBrandID());
		
		iCommodityDetailImgService.save(commodityDetailImgs) ;
		
		return new ReturnBean<>(ReturnBean.CODE_SUCCESS, "保存品牌详情图片成功") ;
	}

	@Override
	public ReturnBean<String> delete(Brand brand) {
		Brand dbBrand = brandRepository.findOne(brand.getBrandID()) ;
		if (null == dbBrand) {
			return new ReturnBean<>(-1, "要删除品牌不存在！");
		}
		
		dbBrand.setStatus(0);
		brandRepository.save(dbBrand) ;
		
		return new ReturnBean<>(ReturnBean.CODE_SUCCESS, "删除品牌成功");
	}

	@Override
	public ReturnBean<String> delete(String key) {
		Brand brand = brandRepository.findOne(key) ;
		brand.setStatus(0);
		brandRepository.save(brand) ;
		return new ReturnBean<>(ReturnBean.CODE_SUCCESS, "删除品牌成功");
	}

	@Override
	public Brand findOne(String key) {
		
		return brandRepository.findOne(key) ;
	}

	@Override
	public ReturnBean<List<Brand>> save(List<Brand> entityList) {
		return new ReturnBean<>(ReturnBean.CODE_SUCCESS, "保存品牌列表成功" , (List<Brand>)brandRepository.save(entityList));
	}

	@Override
	public ReturnBean<PageWrapper<Brand>> findAllBrandByStatus(String brandName, String companyName, Integer pageNum,Integer pageSize ,Integer status) {
		Condition condition = new Condition();
        List<FilterCondition> filterConditions = new ArrayList<FilterCondition>();
        
    		filterConditions.add(new FilterCondition(status.toString(), "status", ConditionRelate.and.name(), Operation.eq.name(), 1)) ;
    		if (null != brandName) {
        		filterConditions.add(new FilterCondition(brandName , "brandName", ConditionRelate.and.name(), Operation.like.name(), 2)) ;
        }
    		if (null != companyName) {
        		filterConditions.add(new FilterCondition("%" + companyName + "%", "companyName", ConditionRelate.and.name(), Operation.like.name(), 3)) ;
        }
    		
    		condition.setFilterCondition(filterConditions);
    		
		Pageable pageable = new PageRequest(pageNum - 1, pageSize);
		Page<Brand> page;
		
		condition = ConditionUtil.buildCondition(XiNiaoJSON.toJSONString(condition), Brand.class).getResult();
		
		page = brandRepository.findAll(condition.getSpecification(), pageable);
		
		List<Brand> brands = page.getContent() ;
		if (null != brands && !brands.isEmpty()) {
			for(Brand brand:brands) {
				brand.setCommodityDetailImgs(iCommodityDetailImgService.findByCommodityID(brand.getBrandID()));
			}
		}
		
		return new ReturnBean<>(ReturnBean.CODE_SUCCESS, "获取品牌列表成功" ,PageHelper.toPage(page));
	}

	@Override
	public ReturnBean<Brand> getBrand(String brandID) {
		Brand brand = brandRepository.findOne(brandID) ;
		
		brand.setCommodityDetailImgs(iCommodityDetailImgService.findByCommodityID(brand.getBrandID())) ;
		
		return new ReturnBean<>(ReturnBean.CODE_SUCCESS, "保存品牌详情图片成功" , brand);
	}

	@Override
	public ReturnBean<List<Brand>> findAllBrandByStatus(Integer status) {
		return new ReturnBean<>(ReturnBean.CODE_SUCCESS, "获取品牌列表成功" , brandRepository.findAllByStatus(status)) ;
	}

	
}
